package com.alibaba.citrus.cr.price.ability.functions.factory.activity;

import com.alibaba.citrus.cr.price.repository.ActivityQueryRepository;
import com.alibaba.citrus.cr.common.util.SpringContextUtil;
import com.alibaba.citrus.cr.price.ability.functions.entity.activity.SegmentedActivityCombinationEntity;
import com.alibaba.cz.base.tool.log.Log;
import com.epoch.app.crpricecenter.model.dto.SegmentedActivityCombinationSDO;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: qiaojiange
 * @create: 2021/11/18
 * @Description 坎级活动组合表工厂
 */
public class SegmentedActivityCombinationFactory {

    private static Log log = Log.getLogger(SegmentedActivityCombinationFactory.class);

    /**
     * 查询资源
     */
    private static ActivityQueryRepository queryRepository = SpringContextUtil.getBean(ActivityQueryRepository.class);

    /**
     * 查询活动组合列表
     *
     * @param activityId          活动 ID
     * @param activityTermId      活动项目 ID
     * @param segmentedActivityId 坎级活动 ID
     * @return
     */
    public static List<SegmentedActivityCombinationEntity> create(String activityId, String activityTermId, String segmentedActivityId) {
        List<SegmentedActivityCombinationSDO> segmentedActivityCombinationSDOList =
                queryRepository.querySegmentedActivityCombinationList(activityId, activityTermId, segmentedActivityId);

        List<SegmentedActivityCombinationEntity> result = new ArrayList<>();
        if (CollectionUtils.isEmpty(segmentedActivityCombinationSDOList)) {
            return result;
        }

        segmentedActivityCombinationSDOList.forEach(e -> {
            SegmentedActivityCombinationEntity entity = SegmentedActivityCombinationEntity.of(e);
            result.add(entity);
        });
        return result;
    }

    /**
     * 通过原品 id 进行创建
     *
     * @param activityId     活动 ID
     * @param activityTermId 活动项目 ID
     * @return
     */
    public static List<SegmentedActivityCombinationEntity> createByItemId(String activityId, String activityTermId) {
        List<SegmentedActivityCombinationSDO> segmentedActivityCombinationSDOList =
                queryRepository.querySegmentedActivityCombinationList(activityId, activityTermId);

        List<SegmentedActivityCombinationEntity> result = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(segmentedActivityCombinationSDOList)) {
            segmentedActivityCombinationSDOList.forEach(e -> {
                result.add(SegmentedActivityCombinationEntity.of(e));
            });
        }

        return result;
    }
}
